//
//  camerViewController.m
//  FaceTest
//
//  Created by chabao on 2018/4/27.
//

#import "camerViewController.h"

#import <Photos/PHPhotoLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>


@interface camerViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@end

@implementation camerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self shooting];
}

//  拍摄照片
- (void)shooting{
    
    UIImagePickerController *pickerCon = [[UIImagePickerController alloc]init];
    pickerCon.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    pickerCon.mediaTypes = @[(NSString *)kUTTypeMovie, (NSString *)kUTTypeImage];
    pickerCon.cameraDevice = UIImagePickerControllerCameraDeviceRear;//设置相机后摄像头
    pickerCon.videoMaximumDuration = 10;//最长拍摄时间
    pickerCon.videoQuality = UIImagePickerControllerQualityTypeHigh;//拍摄质量
    
    pickerCon.allowsEditing = NO;//是否可编辑
    pickerCon.delegate = self;
    [self presentViewController:pickerCon animated:YES completion:nil];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    NSLog(@"取消了");
    [picker dismissViewControllerAnimated:NO completion:^{
        [self dismissViewControllerAnimated:YES completion:nil];
    }];

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {//如果是拍照
        UIImage *image;
        //如果允许编辑则获得编辑后的照片，否则获取原始照片
        if (picker.allowsEditing) {
            image=[info objectForKey:UIImagePickerControllerEditedImage];//获取编辑后的照片
        }else{
            image=[info objectForKey:UIImagePickerControllerOriginalImage];//获取原始照片
        }
        NSLog(@"拍照");
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);//保存到相簿
    }else if([mediaType isEqualToString:(NSString *)kUTTypeMovie]){//如果是录制视频
        NSLog(@"video...");
        NSURL *url=[info objectForKey:UIImagePickerControllerMediaURL];//视频路径
        NSString *urlStr=[url path];
        // 视频的base64字符串
        NSData *data = [NSData dataWithContentsOfFile: urlStr];
                NSString *_encodedImageStr =  [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        // 获得视频的长度
        AVURLAsset * asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:urlStr]];
        CMTime   time = [asset duration];
        int seconds = ceil(time.value/time.timescale);
        NSLog(@"视频的长度是: %d", seconds);
        if (seconds < 3 ) {
            NSLog(@"长度太短");
        } else if (seconds > 30) {
            
        }
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr)) {
            //保存视频到相簿，注意也可以使用ALAssetsLibrary来保存
//            UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);//保存视频到相簿
        }
    }
    
    [picker dismissViewControllerAnimated:NO completion:^{
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
